package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.*;
import org.rcloud.io.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2019/3/4.
 */
@RestController
@RequestMapping(value = "/Blind_date")
public class Blind_dateController {
    @Autowired
    private Blind_dateService blind_dateService;
    @Autowired
    private TravelapplyService travelapplyService;
    @Autowired
    private ShootService shootService;
    @Autowired
    private DiscoverService discoverService;
    @Autowired
    private CountryService countryService;

    /*
    * 结亲报名，报名成功后发送消息模板
    *
    * */
    @RequestMapping(value = "/add",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public int add(@RequestBody String params){
        System.out.println("=======params=======:"+params);
        Blind_date blind_date1 = JSONObject.parseObject(params,Blind_date.class);
        Blind_date blind_date2 = (Blind_date) blind_dateService.selectByKey(blind_date1).getInfo();
        if (null!=blind_date2){
            System.out.println("=======已报名，不能重复报名！=======");
            return 2;
        }else {
            System.out.println("=======未报名，开始报名....=======");
            JSONObject object = JSONObject.parseObject(params);
            object.put("id", UUID.randomUUID());
            object.put("createtime", new Date());
            object.put("flag", 1);
            Blind_date blind_date = JSONObject.parseObject(object.toString(), Blind_date.class);
            // 如果报名成功就发送模板消息
//            Object info = blind_dateService.insertSelective(blind_date).getInfo();
            if (Integer.valueOf(blind_dateService.insertSelective(blind_date).getInfo().toString()) ==1){//满足条件时，已经往数据库插入了
                System.out.println("=======params=======");
                WxPushServiceQcl wxPushServiceQcl=new WxPushServiceQcl();
                wxPushServiceQcl.pushOneUser(blind_date.getUserid(),blind_date.getInfo1(),
                        blind_date.getName(),blind_date.getCreatetime(),blind_date.getInfo2(),
                        blind_date.getInfo3(),blind_date.getInfo4());
                System.out.println("=======报名成功，发送消息模板！=======");
                return 1;
            }else {
                System.out.println("=======报名失败！=======");
                return 0;
            }
//            return blind_dateService.insertSelective(blind_date);
        }
    }

     /*
    * 修改
    *
    * */

    @RequestMapping(value = "/update", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public ResultVo update(@RequestBody String params) {
        Blind_date blind_date = JSONObject.parseObject(params, Blind_date.class);
        return blind_dateService.updateByPrimaryKey(blind_date);
    }

     /*
    * 查询接亲报名、旅游报名、摄影报名、寻美评美、田园报名记录
    *
    * */

    @RequestMapping(value = "/selectByLog", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public JSONObject selectLog(@RequestBody String params) {
        Blind_date blind_date = JSONObject.parseObject(params,Blind_date.class);
        List<Blind_date> blind_dateList = blind_dateService.selectByPrimaryKey(blind_date).getInfo();
        Travelapply travelapply = JSONObject.parseObject(params,Travelapply.class);
        List<Travelapply> travelapplyList = travelapplyService.selectById(travelapply).getInfo();
        Shoot shoot = JSONObject.parseObject(params,Shoot.class);
        List<Shoot> shootList = shootService.selectByPrimaryKey(shoot).getInfo();
        Discover discover = JSONObject.parseObject(params,Discover.class);
        List<Discover> discoverList = discoverService.selectByid(discover).getInfo();
        Country country = JSONObject.parseObject(params,Country.class);
        List<Country> countryList = countryService.selectByid(country).getInfo();
//        J SONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Blind_date",blind_dateList);
        jsonObject.put("Travelapply",travelapplyList);
        jsonObject.put("Shoot",shootList);
        jsonObject.put("Discover",discoverList);
        jsonObject.put("Country",countryList);
//        jsonArray.add(jsonObject);//把JSON对象放到JSONArray数组中
        return jsonObject;


    }

    /*
   * 查询是否参与接亲
   *
   * */
    @RequestMapping(value = "/select", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    //因为返回值是true或false，所以类型改为Boolean
    public Boolean select(@RequestBody String params) {
//        System.out. println("=======11======:"+params);
        Blind_date blind_date = JSON.parseObject(params, Blind_date.class);
        List<Blind_date> blind_date1 = blind_dateService.selectByPrimaryKey(blind_date).getInfo();
        if (null != blind_date1) {//查询是否参与接亲，已参与就返回true
            System.out.println("=======true======");
            return true;
        } else {
            System.out.println("=======false======");
            return false;
        }
    }

    /*
   * 分页查询
   *
   * */
    @RequestMapping(value = "/selectByOther", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public PageInfo<Blind_date> selectByBlind_date(@RequestBody JSONObject params) {
        JSONObject object = JSONObject.parseObject(params.toString());
        Map map = JSONObject.parseObject(object.get("params").toString());
        int pageNum = Integer.parseInt(object.get("pageNum").toString());
        int pageSize = Integer.parseInt(object.get("pageSize").toString());
        PageInfo<Blind_date> pageInfo = blind_dateService.selectByBlind_date(pageNum,pageSize,map);
        return pageInfo;
    }

    /**
     * 查询所有报名数据
     * */
    @RequestMapping(value = "/selectAll", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public PageVo<Blind_date> selectAll(){
        return blind_dateService.selectAll();
    }


    /**
     * 根据时间查询报名数据条数
     * */
    @RequestMapping(value = "/selectBytime", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public ResultVo selectBytime(@RequestBody String params){
        String[] ids = params.split("=");
        System.out.println(ids[0]);
        //今日新增
        Integer a = (Integer) blind_dateService.selectBytime(ids[0]).getInfo();
        //洞桥统计
        Integer dq = (Integer) blind_dateService.selectByNamedq(ids[0]).getInfo();
        //万市统计
        Integer ws = (Integer) blind_dateService.selectByNamews(ids[0]).getInfo();
        //新登统计
        Integer xd = (Integer) blind_dateService.selectByNamexd(ids[0]).getInfo();
        //胥口统计
        Integer xk = (Integer) blind_dateService.selectByNamexk(ids[0]).getInfo();

        JSONObject json = new JSONObject();
        HashMap hashMap = new HashMap();
        hashMap.put("today",a);
        hashMap.put("dq",dq);
        hashMap.put("ws",ws);
        hashMap.put("xd",xd);
        hashMap.put("xk",xk);
        return ResultVo.setOK(hashMap);
    }

    /**
     * 查询所有洞桥报名数据
     * */
    @RequestMapping(value = "/selectAllDQ", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public PageVo<Blind_date> selectAllDQ(){
        return blind_dateService.selectAllDQ();
    }


    /**
     * 查询所有万市报名数据
     * */
    @RequestMapping(value = "/selectAllWS", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public PageVo<Blind_date> selectAllWS(){
        return blind_dateService.selectAllWS();
    }

    /**
     * 查询所有新登报名数据
     * */
    @RequestMapping(value = "/selectAllXD", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public PageVo<Blind_date> selectAllXD(){
        return blind_dateService.selectAllXD();
    }

    /**
     * 查询所有胥口报名数据
     * */
    @RequestMapping(value = "/selectAllXK", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public PageVo<Blind_date> selectAllXK(){
        return blind_dateService.selectAllXK();
    }

    @RequestMapping(value = "/addCertificate",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public ResultVo addCertificate(String id, String info9){
        Blind_date blindDate = new Blind_date();
        blindDate.setId(id);
        blindDate.setInfo9(info9);
        return blind_dateService.addCertificate(blindDate);
    }

 /*   @RequestMapping(value = "/delCertificate",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public ResultVo delCertificate(Blind_date blindDate){
        return blind_dateService.delCertificate(blindDate);
    }*/

    @RequestMapping(value = "/findAllYes",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public PageInfo<Blind_date> selectYes(Integer pagenum) {
        Integer pagesize = 15;
        return blind_dateService.selectYes(pagenum, pagesize);
    }

    @RequestMapping(value = "/findAllNo",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public PageInfo<Blind_date> selectNo(Integer pagenum) {
        Integer pagesize = 15;
        return blind_dateService.selectNo(pagenum, pagesize);
    }

    @RequestMapping(value = "/findlike",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public PageVo<Blind_date> selectLike(String enname) {
        return blind_dateService.selectLike(enname);
    }


}
